﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ZLFMSTInputDialog
{
    public partial class InputBox_Int : Form
    {
        public InputBox_Int(int fDefaultValue, int fMinValue, int fMaxValue, string fCaption, string fTitle)
        {
            InitializeComponent();
            numericUpDown1.Maximum = fMaxValue;
            numericUpDown1.Minimum = fMinValue;
            if (fDefaultValue < fMinValue)
                numericUpDown1.Value = fMinValue;
            else if (fDefaultValue > fMaxValue)
                numericUpDown1.Value = fMaxValue;
            else
                numericUpDown1.Value = fDefaultValue;
            label1.Text = fCaption;
            Text = fTitle;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            Close();
        }

        public static bool InputIntBox(ref int fValue, int fMinValue, int fMaxValue, string fCaption, string fTitle)
        {
            InputBox_Int fInputForm = new InputBox_Int(fValue, fMinValue, fMaxValue, fCaption, fTitle);
            if (fInputForm.ShowDialog()== DialogResult.OK)
            {
                fValue = (int)fInputForm.numericUpDown1.Value;
                return true;
            }
            return false;
        }

        public static bool InputIntBoxWord(ref int fValue, string fCaption)
        {
            return InputIntBox(ref fValue, 0, 0xFFFF, fCaption, "输入2字节整数");
        }

        public static bool InputIntBoxByte(ref int fValue, string fCaption)
        {
            return InputIntBox(ref fValue, 0, 0xFF, fCaption, "输入1字节整数");
        }

        public static bool InputIntBoxbit(ref int fValue, string fCaption)
        {
            return InputIntBox(ref fValue, 0, 1, fCaption, "输入0或1");
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            labelHexValue.Text = "0x" + ((int)numericUpDown1.Value).ToString("X4");
        }
    }
}
